package com.liangsui.mss.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liangsui.mss.constants.Constants;
import com.liangsui.mss.domain.Menu;
import com.liangsui.mss.domain.SimpleUser;
import com.liangsui.mss.dto.MenuDto;
import com.liangsui.mss.mapper.MenuMapper;
import com.liangsui.mss.mapper.RoleMapper;
import com.liangsui.mss.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
* @author zhang
* @description 针对表【sys_menu(菜单权限表)】的数据库操作Service实现
* @createDate 2022-12-04 14:30:19
*/
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService{

    @Autowired
    private MenuMapper menuMapper;

    @Autowired
    private RoleMapper roleMapper;

    /**
     * 查询菜单信息
     * 如查用户是超级管理员，那么查询所有菜单和权限
     * 如果用户是普通用户，那么根据用户ID关联角色和权限
     *
     * @param isAdmin    true  超级管理员  | false 普通管理员
     * @param simpleUser 封装用户id 用户 名称
     * @return menu
     */
    @Override
    public List<Menu> selectMenuTree(boolean isAdmin, SimpleUser simpleUser) {
        QueryWrapper<Menu> menu = new QueryWrapper<>();
        menu.eq(Menu.COL_STATUS, Constants.STATUS_TRUE);
        menu.in(Menu.COL_MENU_TYPE, Constants.MENU_TYPE_C, Constants.MENU_TYPE_M,
                Constants.MENU_TYPE_F);
        menu.orderByAsc(Menu.COL_PARENT_ID);
        if (isAdmin) {
            return menuMapper.selectList(menu);
        } else {
            // 根据用户ID查询用有的菜单
            //return menuMapper.selectList(menu);
            return menuMapper.selectMenuListByUserId(simpleUser.getUserId());
        }
    }

    /**
     * @param menuDto
     * @功能描述:查询全部菜单数据
     */
    @Override
    public List<Menu> listAllMenus(MenuDto menuDto) {
        QueryWrapper<Menu> qw = new QueryWrapper<>();
        qw.like(StringUtils.isNotBlank(menuDto.getMenuName()), Menu.COL_MENU_NAME, menuDto.getMenuName());
        qw.like(StringUtils.isNotBlank(menuDto.getStatus()), Menu.COL_STATUS, menuDto.getStatus());
        return this.menuMapper.selectList(qw);
    }

    @Override
    public Menu getMenuById(Integer id) {
        return this.menuMapper.selectById(id);
    }

    @Override
    public int addMenu(MenuDto menuDto) {
        Menu menu = new Menu();
        BeanUtil.copyProperties(menuDto, menu);
        menu.setCreateBy(menuDto.getSimpleUser().getUserName());
        menu.setCreateTime(DateUtil.date());
        return this.menuMapper.insert(menu);
    }

    @Override
    public int deleteMenuById(Long asList) {
        // 1.删除与之对应的菜单角色
        this.roleMapper.deleteRoleMenuByMenuIds(Arrays.asList(asList));
        // 2.删除菜单
        return this.menuMapper.deleteById(asList);
    }


    @Override
    public int updateMenu(MenuDto menuDto) {
        Menu menu = new Menu();
        BeanUtil.copyProperties(menuDto, menu);
        menu.setUpdateBy(menuDto.getSimpleUser().getUserName());
        menu.setUpdateTime(DateUtil.date());
        return this.menuMapper.updateById(menu);
    }

    @Override
    public boolean hasChilenByMenuID(Long menuid) {
        Long count = this.menuMapper.queryMenuByIDForChilen(menuid);
        return count > 0L ? true : false;
    }

    @Override
    public List<Long> getMenuIdsByRoleId(Long roleId) {
        return this.menuMapper.queryMenuIdsByRoleId(roleId);
    }

    /*********************************************动态路由菜单********************************************/
    @Override
    public List<Map<String, Object>> dynamicMenu() {
        List<Map<String, Object>> list = new ArrayList<>();
        QueryWrapper<Menu> qw = new QueryWrapper<>();
        qw.eq(Menu.COL_STATUS, Constants.STATUS_TRUE);
        qw.in(Menu.COL_MENU_TYPE, Constants.MENU_TYPE_M, Constants.MENU_TYPE_C);
        qw.orderByAsc(Menu.COL_PARENT_ID);
        List<Menu> sysMenus = menuMapper.selectList(qw);
        for (Menu sysMenu : sysMenus) {
            //拼接父菜单
            if (sysMenu.getParentId() == 0) {
                Map<String, Object> map = new HashMap<>();
                map.put("path", sysMenu.getPath());
                map.put("component", "Layout");
                map.put("redirect", "noRedirect");
                map.put("alwaysShow", true);
                map.put("name", sysMenu.getPath());
                //拼接图标
                Map<String, Object> icon = new HashMap<>();
                icon.put("title", sysMenu.getMenuName());
                icon.put("icon", "lock");
                map.put("meta", icon);
                //查询出父菜单的儿子菜单
                List<Map<String, Object>> chiledList = new ArrayList<>();
                List<Menu> menuByParentId = this.getMenuByParentId(sysMenu.getMenuId());
                //组装开始
                for (Menu menu : menuByParentId) {
                    Map<String, Object> childMap = new HashMap<>();
                    childMap.put("path", menu.getPath());
                    childMap.put("component", menu.getPath());
                    childMap.put("name", menu.getPath());
                    Map<String, Object> childIcon = new HashMap<>();
                    //儿子图标
                    childIcon.put("title", menu.getMenuName());
                    childIcon.put("icon", "lock");
                    childMap.put("meta", childIcon);
                    chiledList.add(childMap);

                }
                map.put("children", chiledList);
                list.add(map);
            }

        }
        return list;
    }


    public List<Menu> getMenuByParentId(Long id) {
        QueryWrapper<Menu> qw = new QueryWrapper<>();
        qw.eq(Menu.COL_STATUS, Constants.STATUS_TRUE);
        qw.in(Menu.COL_MENU_TYPE, Constants.MENU_TYPE_M, Constants.MENU_TYPE_C);
        qw.eq(Menu.COL_PARENT_ID, id);
        return menuMapper.selectList(qw);
    }
}




